var parentFuncs, funcs;
var curMenuId;
jQuery(function($) {
	var menu_html;
	$
			.ajax({
				type : "GET",
				url : "../user/loadMain.do",
				dataType : "json",
				success : function(data) {
					if (data.status == "SUCCESS") {
						menu_html = "";
						parentFuncs = data.resBody.parentFuncs;
						funcs = data.resBody.funcs;
						
						for ( var i = 0; i < parentFuncs.length; i++) {
							if (i == 0) {
								menu_html = menu_html + '<li class="">';
							} else {
								menu_html = menu_html + '<li class="">';
							}
							menu_html = menu_html
									+ '<a href="#" class="dropdown-toggle"><i class="menu-icon fa '
									+ parentFuncs[i].imgClass
									+ '"></i><span class="menu-text">'
									+ parentFuncs[i].functionName
									+ '</span><b class="arrow fa fa-angle-down"></b></a><b class="arrow"></b>';
							menu_html = menu_html + '<ul class="submenu">';
							for ( var j = 0; j < funcs.length; j++) {
								if (funcs[j].parentId == parentFuncs[i].id) {
									menu_html = menu_html
											+ '<li class=""><a class="urlRdrct" href="#"><i class="menu-icon fa '
											+ funcs[j].imgClass
											+ '"></i> '
											+ funcs[j].functionName
											+ '<input class="urlRdrctVal" type="hidden" value="'+funcs[j].id+'" title="'+funcs[j].functionUrl+'" /></a><b class="arrow"></b></li>';
								}
							}
							menu_html = menu_html + '</ul></li>';
							$("#funcs_list").html(menu_html);
						}
						regMenuEvent();
					}
				}
			});
	
	//左边菜单的当前选中标识
	$("#funcs_list>li li").click(function(){
		$("#funcs_list li.active").removeClass("active");
		//$(this).addClass("active").closest("ul").closest("li").addClass("active");
		$(this).addClass("active");
	});
	
	//在弹出删除提示框或者弹出自定义修改框时，以下代码确保弹出框的表头可以加html代码
	$.widget("ui.dialog", $.extend({}, $.ui.dialog.prototype, {
		_title: function(title) {
			var $title = this.options.title || '&nbsp;';
			if( ("title_html" in this.options) && this.options.title_html == true ){
				title.html($title);
			}else{ title.text($title);}
		}
	}));
});

function changeMsgBoxShow(msgType,msgInfo){
	if(msgType == "I"){
		$("#returnInfoTip_type").attr("class","alert alert-block alert-success");
		$("#returnInfoTip_icon").html("<i class='ace-icon fa fa-check'></i>");
	}else if(msgType == "E"){
		$("#returnInfoTip_type").attr("class","alert alert-danger");
		$("#returnInfoTip_icon").html("<i class='ace-icon fa fa-bell'></i>");
	}
	$("#returnInfoTip_msg").text(msgInfo);
	$("#returnInfoTip").removeClass("hide");
}

function regMenuEvent() {
	var menuObj;
	// 左边菜单的点击事件
	$("#funcs_list a.urlRdrct").click(function() {
		//设置菜单的选中状态(上面已经有同样的方法做了处理，先注释掉)
//		$("#funcs_list").find("li.active").removeClass("active");
//		$(this).closest("li").addClass("active");
		
		menuObj = $(this).find("input.urlRdrctVal").first();
		curMenuId = menuObj.val();
		
		var urlStr = menuObj.attr("title");
		$.ajax({
			url : urlStr,
			dataType : "html",
			success : function(data) {
				$("#main_content").html(data);
				
				//更新页面的导航信息
				var funcTemp;
				for ( var i = 0; i < funcs.length; i++) {
					funcTemp = funcs[i];
					if (funcTemp.id == curMenuId) {
						$("#secondRange").text(funcTemp.functionName);
						if (funcTemp.parentId != null) {
							for ( var j = 0; j < parentFuncs.length; j++) {
								if (parentFuncs[j].id == funcTemp.parentId) {
									$("#firstRange").text(parentFuncs[j].functionName);
								}
							}
						}
					}
				}
			}
		});
	});
}

function popTipInfo(tipInfo,type){
	if (type == "I") {
		$.gritter.add({
			title: '',
			text: tipInfo,
			//image: '',
			class_name: 'gritter-info gritter-center',// gritter-light'
			sticky: false,//设置是否需要手动关闭
			time: '1000'
		});
	}else if (type == "E") {
		$.gritter.add({
			title: '',
			text: tipInfo,
			//image: '',
			class_name: 'gritter-error gritter-center',// gritter-light'
			sticky: false,//设置是否需要手动关闭
			time: '1000'
		});
	}else{
		$.gritter.add({
			title: '',
			text: tipInfo,
			//image: '',
			class_name: 'gritter-info gritter-center',// gritter-light'
			sticky: false,//设置是否需要手动关闭
			time: '1000'
		});
	}
}

//replace icons with FontAwesome icons like above,更换分页栏的图表
function updatePagerIcons(table) {
	var replacement = 
	{
		'ui-icon-seek-first' : 'ace-icon fa fa-angle-double-left bigger-140',
		'ui-icon-seek-prev' : 'ace-icon fa fa-angle-left bigger-140',
		'ui-icon-seek-next' : 'ace-icon fa fa-angle-right bigger-140',
		'ui-icon-seek-end' : 'ace-icon fa fa-angle-double-right bigger-140'
	};
	$('.ui-pg-table:not(.navtable) > tbody > tr > .ui-pg-button > .ui-icon').each(function(){
		var icon = $(this);
		var $class = $.trim(icon.attr('class').replace('ui-icon', ''));
		
		if($class in replacement) icon.attr('class', 'ui-icon '+replacement[$class]);
	});
}